package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.StudentAtSchool;
import com.xmy.cultivate.entity.StudentAtSchoolScoreDivide;
import com.xmy.cultivate.service.IStudentAtSchoolScoreDivideService;
import com.xmy.cultivate.service.IStudentAtSchoolService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 学生上学所在学校 前端控制器
 * </p>
 *
 * @author Coly
 * @since 2023-04-25
 */
@RestController
@RequestMapping("/api/studentAtSchool")
@Api("学生所在学校控制器")
public class StudentAtSchoolController {

    @Autowired
    IStudentAtSchoolService studentAtSchoolService;

    @Autowired
    IStudentAtSchoolScoreDivideService iStudentAtSchoolScoreDivideService;

    @Autowired
    IdWorker idWorker;

    @GetMapping("/insertSchool")
    @ApiOperation("学校新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name= "name", value = "学校名称" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "regional" , value = "所在区域" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "gradeSection" , value = "学段" , required = true , dataType = "String")
    })
    @RequestBody
    public Result insertSchool(@RequestParam("name")String name, @RequestParam("regional") String regional, @RequestParam("gradeSection")String gradeSection){

        StudentAtSchool school = new StudentAtSchool();
        if (StringUtils.isBlank(name)){
            return new Result(Code.ERROR,null,"名称不能为空");
        }
        QueryWrapper<StudentAtSchool> studentAtSchoolQueryWrapper = new QueryWrapper<>();
        studentAtSchoolQueryWrapper.eq("name",name.trim());
        Long count = studentAtSchoolService.count(studentAtSchoolQueryWrapper);
        if (count>0){
            return new Result(Code.ERROR,null,"“"+name+"”,已添加过，不要重复添加");
        }


        school.setId(idWorker.nextId());
        school.setName(name.trim());
        school.setRegional(regional);
        school.setGradeSection(Integer.valueOf(gradeSection));
        school.setStatus(Integer.valueOf(1));
        studentAtSchoolService.save(school);
        return new Result(Code.OK,null,"学校数据新增成功~");
    }

    @GetMapping("/updateSchool")
    @ApiOperation("学校信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "学校id" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "name" , value = "学校名" , required = false , dataType =  "String"),
            @ApiImplicitParam(name = "regional" , value = "所在区域" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "gradeSection" , value = "学段" , required = true , dataType = "String")
    })
    @RequestBody
    public Result updataSchool(@RequestParam("id") String id,
                               @RequestParam("name") String name,
                               @RequestParam("regional") String regional,
                               @RequestParam("gradeSection")String gradeSection){
        StudentAtSchool school = studentAtSchoolService.getById(id);
        if(!Objects.equals(name,null)){
            school.setName(name);
        }if(!Objects.equals(regional,null)){
            school.setRegional(regional);
        }if(!Objects.equals(gradeSection,null)){
            school.setGradeSection(Integer.valueOf(gradeSection));
        }
        studentAtSchoolService.updateById(school);
        return new Result(Code.OK,null,"学校信息修改完成~");
    }

    @GetMapping("/deleteSchool")
    @ApiOperation("学校信息删除")
    public Result deleteSchool(@RequestParam("id") String id){
        studentAtSchoolService.removeById(id);
        return new Result(Code.OK,null,"学校信息删除成功~");
    }

    @GetMapping("allSchool")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" , value = "学校名" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "pageNum" , value = "分页数" , required = true, dataType = "String"),
            @ApiImplicitParam(name = "regional" , value = "所在区域" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "pageSize" , value = "分页尺寸" , required = true, dataType = "String"),
            @ApiImplicitParam(name = "gradeSection" , value = "学段" , required = true, dataType = "String"),
    })
    @ApiOperation("分页查询所有学校信息")
    public Result allSchool(@RequestParam Map reMap,
                            @RequestParam(value = "gradeSection",required = false,defaultValue = "") String gradeSection,
                            @RequestParam(value = "yearClassId",required = false,defaultValue = "") String yearClassId){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        String name = (String) reMap.get("name");
        String regional = (String) reMap.get("regional");
        if (StringUtils.isBlank(gradeSection) && StringUtils.isNotBlank(yearClassId)){
            gradeSection = CommonUtil.getGradeSectionForYearClassId(Integer.parseInt(yearClassId)).toString();
        }

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(regional),"regional",regional);
        queryWrapper.eq(StringUtils.isNotBlank(gradeSection),"grade_section",gradeSection);
        queryWrapper.like(StringUtils.isNotBlank(name) , "name",name);
        queryWrapper.orderByDesc("id");
        IPage<StudentAtSchool> iPage = new Page<>(pageNum,pageSize);
        studentAtSchoolService.allStudentScore(iPage,queryWrapper);
        for (StudentAtSchool studentAtSchool:iPage.getRecords()){
            QueryWrapper<StudentAtSchoolScoreDivide> sQueryWrapper = new QueryWrapper<>();
            sQueryWrapper.eq("student_at_school_id",studentAtSchool.getId());
            sQueryWrapper.eq(StringUtils.isNotBlank(gradeSection),"grade_section",gradeSection);
            sQueryWrapper.orderByAsc("score");
            List<StudentAtSchoolScoreDivide> studentAtSchoolScoreDivideList = iStudentAtSchoolScoreDivideService.list(sQueryWrapper);
            studentAtSchool.setScoreDivideList(studentAtSchoolScoreDivideList);
        }

        return new Result(Code.OK,iPage,"学校信息遍历完成~");
    }
}
